/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerateRSAKeysPanel.java
 *
 * Created on 30.01.2009, 19:26:27
 */
package jopensslgui;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author uli
 */
public class GenerateRSAKeysPanel extends javax.swing.JPanel
{

    /** Creates new form GenerateRSAKeysPanel */
    public GenerateRSAKeysPanel()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        selectOutputFileButton = new javax.swing.JButton();
        outputFileField = new javax.swing.JTextField();
        outputFileLabel = new javax.swing.JLabel();
        sizeLabel = new javax.swing.JLabel();
        sizeComboBox = new javax.swing.JComboBox();
        okButton = new javax.swing.JButton();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        encryptCheckbox = new javax.swing.JCheckBox();
        encryptionAlgorithmLabel = new javax.swing.JLabel();
        encryptionAlgorithmComboBox = new javax.swing.JComboBox();

        selectOutputFileButton.setText(i18n.getString("GenerateRSAKeysPanel.selectOutputFileButton.text")); // NOI18N
        selectOutputFileButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectOutputFileButtonselectRandomOutputFileButtonActionPerformed(evt);
            }
        });

        outputFileLabel.setText( i18n.getString("GenerateRSAKeysPanel.outputFileLabel.text")); // NOI18N

        sizeLabel.setText( i18n.getString("GenerateRSAKeysPanel.sizeLabel.text")); // NOI18N

        sizeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "512", "1024", "2048", "4096", "8192", "16384", "32768" }));
        sizeComboBox.setSelectedIndex(2);

        okButton.setText( i18n.getString("GenerateRSAKeysPanel.okButton.text")); // NOI18N
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        passwordLabel.setText( i18n.getString("GenerateRSAKeysPanel.passwordLabel.text")); // NOI18N

        passwordField.setText( i18n.getString("GenerateRSAKeysPanel.passwordField.text")); // NOI18N

        encryptCheckbox.setText( i18n.getString("GenerateRSAKeysPanel.encryptCheckbox.text")); // NOI18N

        encryptionAlgorithmLabel.setText( i18n.getString("GenerateRSAKeysPanel.encryptionAlgorithmLabel.text")); // NOI18N

        encryptionAlgorithmComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "DES", "DES3", "AES128", "AES192", "AES256", "Camellia128", "Camellia192", "Camellia256", "IDEA" }));
        encryptionAlgorithmComboBox.setSelectedIndex(4);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(outputFileLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(sizeLabel)
                                .addGap(55, 55, 55)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(outputFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE)
                                .addGap(4, 4, 4)
                                .addComponent(selectOutputFileButton))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(encryptCheckbox))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(encryptionAlgorithmLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(encryptionAlgorithmComboBox, 0, 156, Short.MAX_VALUE))
                            .addComponent(sizeComboBox, 0, 234, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(passwordLabel))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(95, 95, 95)
                        .addComponent(okButton, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(sizeLabel)
                    .addComponent(sizeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(outputFileLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectOutputFileButton)
                    .addComponent(outputFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(passwordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(encryptCheckbox))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(encryptionAlgorithmLabel)
                    .addComponent(encryptionAlgorithmComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void selectOutputFileButtonselectRandomOutputFileButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed
    {//GEN-HEADEREND:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed
        fileChooser.setSelectedFile(new File(outputFileField.getText()));
        fileChooser.showSaveDialog(this);
        outputFileField.setText(fileChooser.getSelectedFile().getAbsolutePath());
}//GEN-LAST:event_selectOutputFileButtonselectRandomOutputFileButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_okButtonActionPerformed
    {//GEN-HEADEREND:event_okButtonActionPerformed
        try
        {
            Runtime r = Runtime.getRuntime();
            //Get the parameters (must be lowercased for the command line)
            String outFile = outputFileField.getText();
            String encryptionAlgorithm =
                    ((String) encryptionAlgorithmComboBox.getSelectedItem()).toLowerCase();
            String password = new String(passwordField.getPassword());
            //Build the command string
            StringBuilder cmdBuilder = new StringBuilder("openssl genrsa -out " + outFile);
            if (encryptCheckbox.isSelected())
            {
                cmdBuilder.append(" -");
                cmdBuilder.append(encryptionAlgorithm);
            }

            cmdBuilder.append(" " + (String) sizeComboBox.getSelectedItem());

            Process p = r.exec(cmdBuilder.toString());
            OutputStream o = p.getOutputStream();
            //Write the password and terminate with a newline character two times (including verifying)
            o.write(password.getBytes());
            o.write("\n".getBytes());
            o.write(password.getBytes());
            o.write("\n".getBytes());
            o.close();
            p.waitFor();
        }
        catch (InterruptedException ex)
        {
            Logger.getLogger(GenerateRSAKeysPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        catch (IOException ex)
        {
            Logger.getLogger(GenerateRSAKeysPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_okButtonActionPerformed
    private JFileChooser fileChooser = new JFileChooser();
    private ResourceBundle i18n = ResourceBundle.getBundle("jopensslgui/Bundle"); //NOI18N
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox encryptCheckbox;
    private javax.swing.JComboBox encryptionAlgorithmComboBox;
    private javax.swing.JLabel encryptionAlgorithmLabel;
    private javax.swing.JButton okButton;
    private javax.swing.JTextField outputFileField;
    private javax.swing.JLabel outputFileLabel;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JButton selectOutputFileButton;
    private javax.swing.JComboBox sizeComboBox;
    private javax.swing.JLabel sizeLabel;
    // End of variables declaration//GEN-END:variables
}
